//
//  HRShowRegestToolView.m
//  Hear
//
//  Created by mac on 2017/11/20.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRShowRegestToolView.h"
#import <UMSocialCore/UMSocialCore.h>

@interface HRShowRegestToolView ()
@property (nonatomic, copy)LoginFromTypeBlock loginFromTypeBlock;
@end
@implementation HRShowRegestToolView
+(void)showRegestToolView:(LoginFromTypeBlock)loginFromTypeBlock{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    HRShowRegestToolView * showView = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    showView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    id view = [[window subviews] lastObject];
    if([view isKindOfClass:[HRShowRegestToolView class]]){
        
        
        HRShowRegestToolView *shareView = (HRShowRegestToolView *)view;
        [shareView closeView];
        
    }else{
        [window addSubview:showView];
        
        [showView loadView:loginFromTypeBlock];
        
    }
}

- (void)loadView:(LoginFromTypeBlock)loginFromTypeBlock{
    
    _loginFromTypeBlock = loginFromTypeBlock;
    
    self.alpha = 0;
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake((kFullWidth-adjuctFloat(280))/2, (kFullHeight-adjuctFloat(320))/2, adjuctFloat(280), adjuctFloat(320))];
    contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:contentView];
    contentView.layer.cornerRadius = 8;
    contentView.clipsToBounds = YES;
    contentView.alpha = 0;
    
    self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
        self.alpha = 1;
        contentView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
    
    CGFloat topPading = 20;
    
    UIButton *closeButton = [UIButton buttonWithType:0];
    [closeButton setImage:[UIImage imageNamed:@"关闭"] forState:0];
    [closeButton addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:closeButton];
    closeButton.frame = CGRectMake(contentView.width-30, 10, 20, 20);
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"登录Hearer";
    titleLabel.font = CustomUIFont(20);
    titleLabel.textColor = FirstTextColor;
    [contentView  addSubview:titleLabel];
    titleLabel.frame = CGRectMake(0, topPading, contentView.width, 50);
    titleLabel.textAlignment = 1;
    
    UIButton *phoneLoginButton = [UIButton buttonWithType:0];
    phoneLoginButton.frame = CGRectMake(topPading, titleLabel.bottom+topPading, contentView.width-topPading*2, 40);
    phoneLoginButton.tag = 0;
    [phoneLoginButton setTitle:@"已有账号？请登录" forState:0];
    [phoneLoginButton setTitleColor:[UIColor whiteColor] forState:0];
    phoneLoginButton.titleLabel.font = CustomUIFont(14);
    [phoneLoginButton addTarget:self action:@selector(clickType:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:phoneLoginButton];
    phoneLoginButton.backgroundColor = NavTinColor;
    phoneLoginButton.layer.cornerRadius = phoneLoginButton.width/12;
    phoneLoginButton.clipsToBounds = YES;
    
    
    UIButton *regestPhoneLoginButton = [UIButton buttonWithType:0];
    regestPhoneLoginButton.tag = 1;
    regestPhoneLoginButton.frame = CGRectMake(phoneLoginButton.left, phoneLoginButton.bottom+topPading, phoneLoginButton.width, phoneLoginButton.height);
    [regestPhoneLoginButton setTitle:@"注册新账号" forState:0];
    [regestPhoneLoginButton setTitleColor:NavTinColor forState:0];
    regestPhoneLoginButton.titleLabel.font = CustomUIFont(14);
    [regestPhoneLoginButton addTarget:self action:@selector(clickType:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:regestPhoneLoginButton];
    regestPhoneLoginButton.layer.cornerRadius = regestPhoneLoginButton.width/12;
    regestPhoneLoginButton.clipsToBounds = YES;
    regestPhoneLoginButton.layer.borderColor = NavTinColor.CGColor;
    regestPhoneLoginButton.layer.borderWidth = 1;
    

    
    UILabel *otherLabel = [UILabel new];
    otherLabel.text = @"使用第三方登录";
    otherLabel.font = CustomUIFont(13);
    otherLabel.textColor = SecondTextColor;
    [contentView  addSubview:otherLabel];
    otherLabel.textAlignment = 1;
    otherLabel.frame = CGRectMake((contentView.width-100)/2, regestPhoneLoginButton.bottom+topPading*2, 100, 20);
    otherLabel.hidden = ![[[NSUserDefaults standardUserDefaults]objectForKey:@"IsShowCash"] intValue];

    
    UILabel *leftLine = [UILabel new];
    leftLine.backgroundColor = SecondTextColor;
    [contentView  addSubview:leftLine];
    leftLine.frame = CGRectMake(regestPhoneLoginButton.left, otherLabel.centerY, otherLabel.left-regestPhoneLoginButton.left-topPading, 0.5);
    
    UILabel *rigthLine = [UILabel new];
    rigthLine.backgroundColor = SecondTextColor;
    [contentView  addSubview:rigthLine];
    rigthLine.frame = CGRectMake(otherLabel.right+topPading, leftLine.top, leftLine.width, 0.5);
    
    UIButton *wxButton = [UIButton buttonWithType:0];
    [wxButton setImage:[UIImage imageNamed:@"微信登录"] forState:UIControlStateNormal];
    [wxButton addTarget:self action:@selector(clickType:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:wxButton];
    wxButton.tag = 2;
    wxButton.hidden = ![[[NSUserDefaults standardUserDefaults]objectForKey:@"IsShowCash"] intValue];
    wxButton.frame = CGRectMake(contentView.width/2-60, contentView.height-55, 40, 40);
    
    UIButton *qqButton = [UIButton buttonWithType:0];
    [qqButton setImage:[UIImage imageNamed:@"QQ登录"] forState:UIControlStateNormal];
    [qqButton addTarget:self action:@selector(clickType:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:qqButton];
    qqButton.tag = 3;
    qqButton.hidden = ![[[NSUserDefaults standardUserDefaults]objectForKey:@"IsShowCash"] intValue];
    qqButton.frame = CGRectMake(contentView.width/2+20,wxButton.top, wxButton.width, wxButton.height);
    
    

}

- (void)clickType:(UIButton *)button{
    self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
        
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        self.loginFromTypeBlock(button.tag);
    }];
}

-(void)closeView
{
    self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
        
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
